package com.zh.demo02.verifycode;

import lombok.Getter;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 验证码抽象对象
 */
@Getter
public abstract class VerifyCode implements Serializable {
    /**验证码值*/
    protected String code;
    /**创建时间*/
    protected long createTime;
    /**有效时长*/
    protected long expireSecond;

    public VerifyCode(){}

    public VerifyCode(String code, int expireSecond) {
        if (code==null || expireSecond<1)
            throw new RuntimeException("传入expireSecond值非法");
        this.code = code;
        this.expireSecond = expireSecond;
        this.createTime = LocalDateTime.now().getSecond();
    }

    protected final boolean isExpire() {
        return (LocalDateTime.now().getSecond() - createTime) > expireSecond;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public void setExpireSecond(long expireSecond) {
        this.expireSecond = expireSecond;
    }

    public void setCode(String code) {
        this.code = code;
    }
}